#include "FMOD_Sound.h"

FMOD_Sound::FMOD_Sound(int maxChannels)
{
	FMOD::System_Create(&system);
	system->init(maxChannels, FMOD_INIT_NORMAL, 0);
}

FMOD_Sound::~FMOD_Sound(void)
{
	for(QMap<QString, FMOD::Sound *>::iterator soundItor = soundMap.begin(); soundItor != soundMap.end(); soundItor++)
	{
		//soundItor->second();
	}
	system->close();
	system->release();
}

void FMOD_Sound::loadSound(QString soundName, QString filePath)
{
	FMOD::Sound * sound;
	FMOD::Channel * channel = 0;

	soundMap[soundName] = sound;
	channelMap[soundName] = channel;

	system->createStream(qPrintable(filePath), FMOD_SOFTWARE | FMOD_LOOP_NORMAL | FMOD_2D, 0, &soundMap[soundName]);
}

void FMOD_Sound::unloadSound(QString soundName)
{
	if(!soundName.isEmpty())
	{
		soundMap[soundName]->release();
	}
}

void FMOD_Sound::addEffect(Effect effect, QString soundName, float value)
{
	switch(effect)
	{
	case Effect::Reverb:
		FMOD::DSP * reverb;
		system->createDSPByType(FMOD_DSP_TYPE_REVERB, &reverb);
		channelMap[soundName]->addDSP(reverb);
		reverb->setParameter(FMOD_DSP_REVERB_ROOMSIZE, value);
		reverbMap[soundName] = reverb;
		break;
	case Effect::Pitch:
		FMOD::DSP * pitch;
		system->createDSPByType(FMOD_DSP_TYPE_PITCHSHIFT, &pitch);
		channelMap[soundName]->addDSP(pitch);
		pitch->setParameter(0, value);
		pitchMap[soundName] = pitch;
		break;
	case Effect::Distortion:
		FMOD::DSP * distortion;
		system->createDSPByType(FMOD_DSP_TYPE_DISTORTION, &distortion);
		channelMap[soundName]->addDSP(distortion);
		distortion->setParameter(0, value);
		distortionMap[soundName] = distortion;
		break;
	case Effect::Echo:
		FMOD::DSP * echo;
		system->createDSPByType(FMOD_DSP_TYPE_ECHO, &echo);
		channelMap[soundName]->addDSP(echo);
		echo->setParameter(0, value);
		echoMap[soundName] = echo;
		break;
	case Effect::Flange:
		FMOD::DSP * flange;
		system->createDSPByType(FMOD_DSP_TYPE_FLANGE, &flange);
		channelMap[soundName]->addDSP(flange);
		flange->setParameter(0, value);
		flangeMap[soundName] = flange;
		break;
	default:
		break;
	}
}
void FMOD_Sound::removeEffect(Effect effect, QString soundName)
{
	switch(effect)
	{
	case Effect::Reverb:
		reverbMap[soundName]->remove();
		break;
	case Effect::Pitch:
		pitchMap[soundName]->remove();
		break;
	case Effect::Distortion:
		distortionMap[soundName]->remove();
		break;
	case Effect::Echo:
		echoMap[soundName]->remove();
		break;
	case Effect::Flange:
		flangeMap[soundName]->remove();
		break;
	default:
		break;
	}
}

void FMOD_Sound::playSound(QString soundName, float volume)
{
	if(!soundName.isEmpty())
	{
		system->playSound(FMOD_CHANNEL_FREE, soundMap[soundName], false, &channelMap[soundName]);
		this->setVolume(qPrintable(soundName), volume);
	}
}

void FMOD_Sound::stopSound(QString soundName)
{
	if(!soundName.isEmpty())
	{
		channelMap[soundName]->stop();
	}
}
float FMOD_Sound::getVolume(QString soundName)
{
	if(!soundName.isEmpty())
	{
		float * volume;
		channelMap[soundName]->getVolume(volume);

		return *volume;

		delete volume;
		volume = 0;
	}
}

void FMOD_Sound::setVolume(QString soundName, float volume)
{
	if(!soundName.isEmpty())
	{
		channelMap[soundName]->setVolume(volume);
	}
}

void FMOD_Sound::setPaused(QString soundName, bool state)
{
	if(!soundName.isEmpty())
	{
		channelMap[soundName]->setPaused(state);
	}
}

void FMOD_Sound::setEffectValue(QString soundName, Effect type, float newValue)
{
	if(!soundName.isEmpty())
	{
		switch(type)
		{
			case Effect::Reverb:
				reverbMap[soundName]->setParameter(FMOD_DSP_REVERB_ROOMSIZE, newValue);
				break;
			case Effect::Pitch:
				pitchMap[soundName]->setParameter(0, newValue);
				break;
			case Effect::Distortion:
				distortionMap[soundName]->setParameter(0, newValue);
				break;
			case Effect::Echo:
				echoMap[soundName]->setParameter(0, newValue);
				break;
			case Effect::Flange:
				flangeMap[soundName]->setParameter(0, newValue);
				break;
		}
	}
}
